/**
 * 场景主控
 */
class ScreenLayer extends egret.DisplayObjectContainer {
    /** 场景盒子 */
    private screenContainer: egret.DisplayObjectContainer[] = [];
    /** 场景序号 */
    private screenNum: number = 0;
    /** 加载菜单框架 */
    private frameMenu: FrameMenu = new FrameMenu();

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        //  this.stage.scaleMode = egret.StageScaleMode.NO_BORDER;
        document.querySelector("body").style.background = "#000";
        /** 加载开始界面 */
        this.loadScreenHanler(new StartScreen());
        /** 加载第一场景 */
        this.loadScreenHanler(new ScreenOne());
        /** 加载第二场景 */
        this.loadScreenHanler(new ScreenTwo());
        /** 加载第三场景 */
        this.loadScreenHanler(new ScreenThree());
        /** 加载完后启动 */
        this.createSecrrnHanler();
    }
    /** 创建结算界面 */
    private createOverScreen() {
        const oldScreen = this.screenContainer[this.screenNum];
        const overScreen: OverScreen = new OverScreen();

        this.addChild(overScreen);
        Main.anchorCenter(overScreen);
        overScreen.x = this.stage.stageWidth * .5;
        overScreen.y = this.stage.stageHeight * .5;
        overScreen.scaleX = 0;
        overScreen.scaleY = 0;

        oldScreen.addEventListener("next", () => {
            console.log("最后一个场景触发Next事件");
            egret.Tween.get(overScreen).to({ scaleX: 1, scaleY: 1 }, 0).call(() => {
                this.removeChild(oldScreen);
                this.removeChild(this.frameMenu);
            });
        }, this);

    }
    /** 加载场景方法 */
    private loadScreenHanler(screenObj) {
        this.screenContainer.push(screenObj);
        screenObj.touchEnabled = true;
        screenObj.addEventListener(egret.TouchEvent.TOUCH_BEGIN, Main.superStopPropagation, this);
        screenObj.addEventListener(egret.TouchEvent.TOUCH_TAP, Main.superStopPropagation, this);
    }
    /** 创建场景方法 */
    private createSecrrnHanler() {
        if (this.screenNum + 1 >= this.screenContainer.length) return this.createOverScreen();

        const oldScreen = this.screenContainer[this.screenNum];
        const newScreen = this.screenContainer[this.screenNum + 1];

        this.addChild(newScreen);
        this.addChild(oldScreen);
        /** 创建新屏幕, 坐标点在屏幕的一半 */
        newScreen.x = this.stage.stageWidth * .5;

        oldScreen.addEventListener("next", () => this.switchScreenHanler(oldScreen, newScreen), this);

    }
    /** 场景切换方法 */
    private switchScreenHanler(oldScreen, newScreen) {
        /** 新屏幕出场方式 */
        egret.Tween.get(newScreen).to({ x: 0 }, 900);
        /** 旧屏幕退场方式 */
        egret.Tween.get(oldScreen).to({ x: -this.stage.stageWidth }, 1000).call(() => {
            this.removeChild(oldScreen);
            this.screenNum++;
            this.createSecrrnHanler();
            /** 加载菜单框架 */
            this.addChild(this.frameMenu);
            this.frameMenu.menuSetting(this.screenNum);
        });
    }
    /** 创建弹窗点击切换到下一个屏幕 */
    public static createAlert(container) {
        const alert_png = Main.createBitmapByName("switch_page_png");
        container.addChild(alert_png);
        Main.anchorCenter(alert_png);
        alert_png.x = container.stage.stageWidth * .5;
        alert_png.y = container.stage.stageHeight * .5;

        alert_png.touchEnabled = true;
        alert_png.addEventListener(egret.TouchEvent.TOUCH_TAP, alertHandler, this);

        function alertHandler() {
            container.dispatchEvent(new egret.Event("next"))
        }
    }
    /** 创建开始弹窗点击开始此场景 */
    public static creatOpenAlert(container, num) {
        console.log(container, num, `openPanle${num}_png`)
        const alert_png = Main.createBitmapByName(`openPanle${num}_png`);
        container.addChild(alert_png);
        Main.anchorCenter(alert_png);
        alert_png.x = container.stage.stageWidth * .5;
        alert_png.y = container.stage.stageHeight * .5;

        alert_png.touchEnabled = true;
        alert_png.addEventListener(egret.TouchEvent.TOUCH_TAP, alertHandler, this);

        function alertHandler() {
            container.removeChild(alert_png);
        }
    }

}